
local Skada = Skada

local mod = Skada:NewModule("Ignite munching")

--  add a deatils bar to skada window
local function add_detail_bar(win, nr, title, value, valuetext)
	local d = win.dataset[nr] or {}
	win.dataset[nr] = d
	
	d.value = 0
	d.label = title
	d.id = title
	d.valuetext = valuetext
end

-- add actual ignite dmg to set
function log_ignite(set, dmg)
    if not set.totalIgniteDmgActual then
        set.totalIgniteDmgActual = 0
    end

    set.totalIgniteDmgActual = set.totalIgniteDmgActual+dmg
end

-- add crit and resulting ignite dmg to set
function log_crit(set, critDmg, igniteDmg)
    if not set.totalCritDmg then
        set.totalCritDmg = 0
        set.totalIgniteDmgDesired = 0
    end
    
    set.totalCritDmg = set.totalCritDmg+critDmg
    set.totalIgniteDmgDesired = set.totalIgniteDmgDesired+igniteDmg
end


-- parse and log combat log
local function SpellDamage(timestamp, eventtype, srcGUID, srcName, srcFlags, dstGUID, dstName, dstFlags, ...)
    -- Spell damage.
    local mastery = ((GetCombatRatingBonus(26)+8)*2.8)
    local igniteDmg = 0
   
    -- check this an event triggert by me
    if srcGUID == UnitGUID("player") then
        if srcGUID ~= dstGUID then
            local spellId, spellName, spellSchool, samount, soverkill, sschool, sresisted, sblocked, sabsorbed, scritical, sglancing, scrushing = ...
            
            -- check for ignite ticks
            if spellId == 12654 then
                log_ignite(Skada.current,samount)
                log_ignite(Skada.total,samount)
            else
                -- we are intersted on fire school damage, but not on combustion
                if spellSchool == 4 and spellId ~= 11129 and spellId ~= 83853 then
                    -- we are only intersted on crits
                    if scritical then
                        -- basic ignite dmg: 40% of crit dmg
                        igniteDmg = (samount/100)*40
                        -- add mastery bonus
                        igniteDmg = igniteDmg + (igniteDmg/100)*mastery
                        -- log it
                        log_crit(Skada.current, samount, igniteDmg)
                        log_crit(Skada.total, samount, igniteDmg)
                    end
                end
            end
        end
    end
end



function mod:Update(win, set)
    local munchedDmg = 0
    local munchedDmgPrec = 0
    if set.totalIgniteDmgActual > 0 then
        munchedDmg = set.totalIgniteDmgDesired-set.totalIgniteDmgActual
         munchedDmgPrec = (100/set.totalIgniteDmgDesired)*munchedDmg
    end

    add_detail_bar(win, 1, "Reference ignite damage", set.totalIgniteDmgDesired, Skada:FormatNumber(set.totalIgniteDmgDesired))
    add_detail_bar(win, 2, "Actual ignite damage", set.totalIgniteDmgActua, Skada:FormatNumber(set.totalIgniteDmgActual))
    add_detail_bar(win, 3, "Munched ignite damage", munchedDmg, Skada:FormatNumber(munchedDmg))
    add_detail_bar(win, 4, "Munched ignite damage (%)", munchedDmgPrec, string.format("%02.1f%%",munchedDmgPrec))
end

function mod:GetSetSummary(set)
	local munchedDmg = 0
    local munchedDmgPrec = 0
    if set.totalIgniteDmgActual > 0 then
        munchedDmg = set.totalIgniteDmgDesired-set.totalIgniteDmgActual
        munchedDmgPrec = (100/set.totalIgniteDmgDesired)*munchedDmg
    end
    
    return string.format("%02.1f%%",munchedDmgPrec)
end

function mod:AddSetAttributes(set)
    if not set.totalCritDmg then
        set.totalCritDmg = 0
        set.totalIgniteDmgDesired = 0
        set.totalIgniteDmgActual = 0
    end
end

function mod:OnEnable()

    Skada:RegisterForCL(SpellDamage, 'DAMAGE_SHIELD', {src_is_interesting = true, dst_is_not_interesting = true})
	Skada:RegisterForCL(SpellDamage, 'SPELL_DAMAGE', {src_is_interesting = true, dst_is_not_interesting = true})
	Skada:RegisterForCL(SpellDamage, 'SPELL_PERIODIC_DAMAGE', {src_is_interesting = true, dst_is_not_interesting = true})
	Skada:RegisterForCL(SpellDamage, 'SPELL_BUILDING_DAMAGE', {src_is_interesting = true, dst_is_not_interesting = true})
	Skada:RegisterForCL(SpellDamage, 'RANGE_DAMAGE', {src_is_interesting = true, dst_is_not_interesting = true})

	Skada:AddMode(self)
end

function mod:OnDisable()
	Skada:RemoveMode(self)
end